package com.google.demo.AsyncTask;

import java.util.ArrayList;
import java.util.List;

import com.google.demo.R;
import com.google.demo.Activity.LoginMainActivity;
import com.google.demo.adapter.HistoryAdapter;
import com.google.demo.model.HistoryItem;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ListView;
public class LoadMoreListViewHistory extends AsyncTask<Void, Void, Void>{
	
	private ProgressDialog mProgressDialog;
	private Context mContext;
	private String MESSAGE_DIALOG = "Please waitting...";
	private int mCurrent_page;
	private List<HistoryItem> mListHistoryItem;
	private ListView mListView;
	public LoadMoreListViewHistory(Context context,ListView listView,List<HistoryItem> listHistoryItem) {
		// TODO Auto-generated constructor stub
		Log.d("-------Datpv---onClick", "LoadMoreListViewHistory");
		mContext = context;
		mCurrent_page = 1;
		mListView = listView;
		mListHistoryItem = listHistoryItem;
	}
	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		Log.d("-------Datpv---onClick", "PreExecute");
		mProgressDialog = new ProgressDialog(mContext); 
		mProgressDialog.setMessage(MESSAGE_DIALOG);
		mProgressDialog.setCancelable(false);
	}
	@Override
	protected Void doInBackground(Void... unused) {
		// TODO Auto-generated method stub
		Log.d("-------Datpv---onClick 1", "doInBackground");
		
		((Activity) mContext).runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.d("-------Datpv---onClick 2", "doInBackground");
				mCurrent_page += 1;
				
				//set position last listview
				int lastPosition = mListView.getLastVisiblePosition();
				
				// Next page request
//		        URL = "http://api.androidhive.info/list_paging/?page=" + current_page;
		//
//		        xml = parser.getXmlFromUrl(URL); // getting XML
//		        doc = parser.getDomElement(xml); // getting DOM element
				
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
				mListHistoryItem.add(new HistoryItem());
			
				HistoryAdapter adapter = new HistoryAdapter(mContext, mListHistoryItem);
				mListView.setAdapter(adapter);
				
				mListView.setSelectionFromTop(lastPosition + 1, 0);
			}
		});
		
		return null;
	}
	@Override
	protected void onPostExecute(Void unused) {
		// TODO Auto-generated method stub
//		super.onPostExecute(result);
		mProgressDialog.dismiss();
		Log.d("-------Datpv---onClick", "postExecute");
	}
}